package com.baoteng.captcha.recognition.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baoteng.captcha.recognition.vo.CaptchaVO;
import com.baoteng.captcha.recognition.vo.VehiCaptVo;

@Service
public class WhiteListServices {

	@Autowired
	RedisTemplate<String, VehiCaptVo> redisTemplate;
	
	public static String rprefix="CP_";
	
	
	public String getCaptchaVo(CaptchaVO c) {
		if(StringUtils.isBlank(c.getVeCode()) || StringUtils.isBlank(c.getInsurecode()) || StringUtils.isBlank(c.getName())) {
			return null;
		}
		VehiCaptVo rc=(VehiCaptVo)	redisTemplate.opsForHash().get(rprefix+c.getInsurecode(), c.getVeCode());
		if(rc!=null) {
			if(c.getName().equalsIgnoreCase("N")&& StringUtils.isNoneBlank(rc.getBusCode())&& StringUtils.isNoneBlank(rc.getBusData()) && rc.getBusData().equals(c.getData())) {
				return rc.getBusCode();
			}else if(c.getName().equalsIgnoreCase("Y")&& StringUtils.isNoneBlank(rc.getForCode())&& StringUtils.isNoneBlank(rc.getForData())&& rc.getForData().equals(c.getData())) {
				return rc.getForCode();
			}
		}
		return null;
	}
	
	public void saveCaptchaVo(CaptchaVO c) {
		if(StringUtils.isBlank(c.getVeCode()) || StringUtils.isBlank(c.getInsurecode()) ||StringUtils.isBlank(c.getText()) || StringUtils.isBlank(c.getName())) {
			return ;
		}
		VehiCaptVo rc=(VehiCaptVo)	redisTemplate.opsForHash().get(rprefix+c.getInsurecode(), c.getVeCode());
		if(rc ==null) {
			rc = new VehiCaptVo();
		}
		if(c.getName().equalsIgnoreCase("N")) {
			rc.setBusCode(c.getText());
			rc.setBusData(c.getData());
		}else if(c.getName().equalsIgnoreCase("Y")) {
			rc.setForCode(c.getText());
			rc.setForData(c.getData());
		}
		
		redisTemplate.opsForHash().put(rprefix+c.getInsurecode(), c.getVeCode(), rc);
	}
}
